package com.sviet.game.tetriswindow.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.sviet.game.tetriswindow.R;
import com.sviet.game.tetriswindow.TetrisTileView;
import com.sviet.game.tetriswindow.util.SharedPreferencesUtils;

public class ControlModeDialog extends Dialog implements OnClickListener, OnCheckedChangeListener {

	private String TAG = ControlModeDialog.class.getSimpleName();
	private Context context;
	private Button bt_ok;
	private Button bt_cancel;
	private CheckBox cb_navigation;
	private CheckBox cb_touch;

	public ControlModeDialog(Context context) {
		super(context);
		// remove background dialog using style
		// super(context, R.style.Theme_Dialog_Translucent);
		// TODO Auto-generated constructor stub

		Log.v(TAG, "ControlModeDialog");
		this.context = context;
		Log.v(TAG, "this.context = " + this.context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.control_mode_dialog);

		// remove background dialog
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.TRANSPARENT));
		
		int control_mode = SharedPreferencesUtils.GetPreference(getContext(), 
				SharedPreferencesUtils.CONTROL_MODE);
		Log.v(TAG, "control_mode = " + control_mode);

		cb_navigation = (CheckBox) findViewById(R.id.control_mode_cb_navigation);
		cb_navigation.setOnCheckedChangeListener(this);
		cb_touch = (CheckBox) findViewById(R.id.control_mode_cb_touch);
		cb_touch.setOnCheckedChangeListener(this);
		
		if (control_mode == TetrisTileView.CONTROL_NAVIGATION) {
			cb_navigation.setChecked(true);
			cb_touch.setChecked(false);
		} else if (control_mode == TetrisTileView.CONTROL_TOUCH) {
			cb_navigation.setChecked(false);
			cb_touch.setChecked(true);
		}
		
		// Buttons
		bt_ok = (Button) findViewById(R.id.control_mode_bt_ok);
		bt_ok.setOnClickListener(this);
		bt_cancel = (Button) findViewById(R.id.control_mode_bt_cancel);
		bt_cancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.control_mode_bt_ok) {
			int control_mode = TetrisTileView.CONTROL_NAVIGATION;
			if (cb_touch.isChecked()) {
				control_mode = TetrisTileView.CONTROL_TOUCH;
			}
			SharedPreferencesUtils.SetPreference(getContext(), 
					SharedPreferencesUtils.CONTROL_MODE, control_mode);
			dismiss();
		} else if (v.getId() == R.id.control_mode_bt_cancel) {
			dismiss();
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (buttonView.getId() == R.id.control_mode_cb_navigation) {
			cb_touch.setChecked(!isChecked);
		} else if (buttonView.getId() == R.id.control_mode_cb_touch) {
			cb_navigation.setChecked(!isChecked);
		}
	}
}
